<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" v-show="showSearch">
      <el-form-item label="索引关键字" prop="dbKey">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入索引关键字"
          size="small"
          v-model="queryParams.dbKey"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入描述"
          size="small"
          v-model="queryParams.description"
        />
      </el-form-item>
      <el-form-item label="驱动" prop="driverClass">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入驱动"
          size="small"
          v-model="queryParams.driverClass"
        />
      </el-form-item>
      <el-form-item label="URL" prop="url">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入URL"
          size="small"
          v-model="queryParams.url"
        />
      </el-form-item>
      <el-form-item label="帐号" prop="dbUser">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入帐号"
          size="small"
          v-model="queryParams.dbUser"
        />
      </el-form-item>
      <el-form-item label="密码" prop="dbPassword">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入密码"
          size="small"
          v-model="queryParams.dbPassword"
        />
      </el-form-item>

      <el-form-item label="数据库名称" prop="dbName">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入数据库名称"
          size="small"
          v-model="queryParams.dbName"
        />

      </el-form-item>
      <el-form-item label="remarks" prop="remarks">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入remarks"
          size="small"
          v-model="queryParams.remarks"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="cyan">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleAdd"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          v-hasPermi="['gen:source:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="single"
          @click="handleUpdate"
          icon="el-icon-edit"
          size="mini"
          type="success"
          v-hasPermi="['gen:source:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          v-hasPermi="['gen:source:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          size="mini"
          type="warning"
          v-hasPermi="['gen:source:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="sourceList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="主键" prop="id"/>
      <el-table-column align="center" label="索引关键字" prop="dbKey"/>
      <el-table-column align="center" label="描述" prop="description"/>
      <el-table-column align="center" label="驱动" prop="driverClass"/>
      <el-table-column align="center" label="URL" prop="url"/>
      <el-table-column align="center" label="帐号" prop="dbUser"/>
      <el-table-column align="center" label="密码" prop="dbPassword"/>
      <el-table-column align="center" label="数据库名称" prop="dbName"/>
      <el-table-column align="center" label="remarks" prop="remarks"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            type="text"
            v-hasPermi="['gen:source:edit']"
          >修改
          </el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
            v-hasPermi="['gen:source:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />

    <!-- 添加或修改数据源对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form :model="form" :rules="rules" label-width="80px" ref="form">
        <el-form-item label="索引关键字" prop="dbKey">
          <el-input placeholder="请输入索引关键字" v-model="form.dbKey"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input placeholder="请输入描述" v-model="form.description"/>
        </el-form-item>
        <el-form-item label="驱动" prop="driverClass">
          <el-input placeholder="请输入驱动" v-model="form.driverClass"/>
        </el-form-item>
        <el-form-item label="URL" prop="url">
          <el-input placeholder="请输入URL" v-model="form.url"/>
        </el-form-item>
        <el-form-item label="帐号" prop="dbUser">
          <el-input placeholder="请输入帐号" v-model="form.dbUser"/>
        </el-form-item>
        <el-form-item label="密码" prop="dbPassword">
          <el-input placeholder="请输入密码" v-model="form.dbPassword"/>
        </el-form-item>

        <el-form-item label="数据库名称" prop="dbName">
          <el-input placeholder="请输入数据库名称" v-model="form.dbName"/>
        </el-form-item>
        <el-form-item label="remarks" prop="remarks">
          <el-input placeholder="请输入remarks" v-model="form.remarks"/>
        </el-form-item>

      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addSource, delSource, getSource, listSource, updateSource} from "@/api/gen/source";

  export default {
    name: "Source",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 数据源表格数据
        sourceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dbKey: null,
          description: null,
          driverClass: null,
          url: null,
          dbUser: null,
          dbPassword: null,
          dbType: null,
          dbName: null,
          remarks: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          dbKey: [
            {required: true, message: "索引关键字不能为空", trigger: "blur"}
          ],
          description: [
            {required: true, message: "描述不能为空", trigger: "blur"}
          ],
          driverClass: [
            {required: true, message: "驱动不能为空", trigger: "blur"}
          ],
          url: [
            {required: true, message: "URL不能为空", trigger: "blur"}
          ],
          dbUser: [
            {required: true, message: "帐号不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询数据源列表 */
      getList() {
        this.loading = true;
        listSource(this.queryParams).then(response => {
          this.sourceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          dbKey: null,
          description: null,
          driverClass: null,
          url: null,
          dbUser: null,
          dbPassword: null,
          dbType: null,
          dbName: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remarks: null,
          delFlag: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加数据源";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getSource(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改数据源";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSource(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSource(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除数据源编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delSource(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('gen/source/export', {
          ...this.queryParams
        }, `gen_source.xlsx`)
      }
    }
  };
</script>
